﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace CHFramework
{
    public struct LanguageInfo
    {
        public string LKey;
        public string LChinese;
        public string LEnglish;
    }

    public class Localizition : MonoSingleton<Localizition>
    {
        private static List<LanguageInfo> LanguageList = null;
        private static LanguageInfo info;

        public Localizition()
        {

        }

        public static string Localize(string key)
        {
            if (LanguageList == null)
                LoadLocalizition();
            //TODO
            for (int i = 0; i < LanguageList.Count; i++)
            {
                LanguageInfo info = LanguageList[i];
                switch (SysDefine.CurLanguage)
                {
                    case Language.Chinese:
                        return info.LChinese;
                    case Language.English:
                        return info.LEnglish;
                }
            }

            return string.Empty;
        }

        private static void LoadLocalizition()
        {
            LanguageList = new List<LanguageInfo>();

            TextAsset csv = ResourcesMgr.Instance.LoadAsset<TextAsset>("Table/Localizition/localizition", false);
            MemoryStream ms = new MemoryStream(csv.bytes);
            TextWriter tw = new StreamWriter(ms);
            tw.Write(csv.text);
            ms.Seek(0, SeekOrigin.Begin);
            using (var reader = new CsvFileReader(ms))
            {
                List<string> columns = new List<string>();
                reader.ReadRow(columns);
                while (reader.ReadRow(columns))
                {
                    if (!string.IsNullOrEmpty(columns[0]))
                        info.LKey = columns[0];
                    if (!string.IsNullOrEmpty(columns[1]))
                        info.LChinese = columns[1];
                    if (!string.IsNullOrEmpty(columns[2]))
                        info.LEnglish = columns[2];

                    LanguageList.Add(info);
                }
            }
        }
    }
}
